package com.hr.GC;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: leetcode
 * @description: GC停顿测试
 * @author: HuRan QQ1345122890
 * @create: 2021-03-27 11:19
 */
public class StopTheWorldTest {
    public static void main(String[] args) {
        PrintThread printThread=new PrintThread();
        printThread.start();
        WorkThread workThread=new WorkThread();
        workThread.start();
    }
    static class WorkThread extends Thread{
        List<byte[]> list=new ArrayList<>();

        @Override
        public void run() {
           while (true){
               for (int i = 0; i < 1000; i++) {
                   byte[] b=new byte[1024];
                   list.add(b);
               }
               if(list.size()>10000){
                   list.clear();
                   System.gc();
               }
           }
        }
    }
     static class PrintThread extends Thread{
        private  final long startTime = System.currentTimeMillis();
        @Override
        public void run() {
            while (true){
                long t=System.currentTimeMillis()-startTime;
                System.out.println(t / 1000 + "." + t % 1000);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}